package com.cby.olive.sample

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.cby.android.olive.ext.*
import com.cby.android.olive.storage.OlivePreferences
import com.cby.android.olive.utils.AppUtils
import com.cby.android.olive.widget.bottomtab.bottom.TabBottomInfo
import com.cby.olive.sample.adapter.MainPageAdapter
import com.cby.olive.sample.databinding.ActivityMainBinding
import com.cby.olive.sample.model.Coordinate
import com.cby.olive.sample.model.Location
import com.cby.olive.sample.model.Person
import com.cby.olive.sample.model.persons

class MainActivity : AppCompatActivity() {

    private val binding by binding<ActivityMainBinding>()

    private val adapter = MainPageAdapter(this@MainActivity)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        initFragment()
        initTabBottom()
        // app设置成灰白
        if(OlivePreferences.getGrayStatus()) {
            this.window.decorView.setGray()
        }
    }

    private fun initFragment() {
        val list = arrayListOf<Fragment>()
        list.add(HomeFragment())
        list.add(LikeFragment())
        list.add(MainFragment())
        list.add(CategoryFragment())
        list.add(MyFragment())
        binding.viewPager.adapter = adapter
        adapter.setData(list)
    }

    private fun initTabBottom() {
        binding.tabBottom.setTabAlpha(1f)
        val bottomInfoList: MutableList<TabBottomInfo<*>> = ArrayList()
        //删除mipmap-anydpi-v26 xml @https://hymane.itscoder.com/bitmap-factory-decode-return-null/
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
//            HiTabInfo info = new HiTabInfo("tab" + i, bitmap, bitmap);
        val homeInfo = TabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            getString(R.string.if_home),
            null,
            "#ff656667",
            "#ffd44949"
        )
        val infoRecommend = TabBottomInfo(
            "收藏",
            "fonts/iconfont.ttf",
            getString(R.string.if_favorite),
            null,
            "#ff656667",
            "#ffd44949"
        )

//        val infoCategory = HiTabInfo(
//            "分类",
//            "fonts/iconfont.ttf",
//            getString(R.string.if_category),
//            null,
//            "#ff656667",
//            "#ffd44949"
//        )

        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.smile, null)

        /*val infoCategory = TabBottomInfo<String>(
            "分类",
            bitmap,
            bitmap
        )*/
        val infoCategory = TabBottomInfo<String>(
            "分类",
            R.drawable.smile,
            R.drawable.smile_selected
        )
        val infoChat = TabBottomInfo(
            "推荐",
            "fonts/iconfont.ttf",
            getString(R.string.if_recommend),
            null,
            "#ff656667",
            "#ffd44949"
        )
        val infoProfile = TabBottomInfo(
            "我的",
            "fonts/iconfont.ttf",
            getString(R.string.if_profile),
            null,
            "#ff656667",
            "#ffd44949"
        )
        bottomInfoList.add(homeInfo)
        bottomInfoList.add(infoRecommend)
        bottomInfoList.add(infoCategory)
        bottomInfoList.add(infoChat)
        bottomInfoList.add(infoProfile)
        binding.tabBottom.inflateInfo(bottomInfoList)
        binding.tabBottom.addTabSelectedChangeListener { index, _, nextInfo ->
            //Toast.makeText(this@MainActivity, nextInfo.name, Toast.LENGTH_SHORT).show()
            binding.viewPager.setCurrentItem(index, true)
        }
        binding.tabBottom.defaultSelected(homeInfo)
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.tabBottom.defaultSelected(bottomInfoList[position.toInt()])
            }
        })

        //变某个tab的高度
        val tabBottom = binding.tabBottom.findTab(bottomInfoList[2])
        tabBottom?.apply { resetHeight(66.dp) }
    }
}